
global myvarlist equity purchaseprice deposit_final loan_final defprice  
global regvar age_borrower i.advtype	gross_income  i.employment

global map = 1
global reg = 1

*------------------------------------------------------------------------------*
* Maps *
*------------------------------------------------------------------------------*

if ${map} == 1 {

use "${datadir}\HTB_main_cleaned_feb2016", replace

drop _merge
gen LAD14CD = laua
sort LAD14CD
merge m:1 LAD14CD using "${datadir}\gbdb.dta" 
tab _merge
keep if _merge==3 | LAD14NM =="City of London" | LAD14NM == "Westminster" | LAD14NM == "Camden" | LAD14NM == "Islington" | ///
					LAD14NM =="Elmbridge" | LAD14NM =="Oxford"

bys id_map: gen tot = _N
bys id_map: gen fo = _n==1


gen london = .
replace london = 0   if region == 5 | region == 6 | LAD14NM =="Elmbridge" | LAD14NM =="Oxford"
replace  london =  1 if LAD14NM == "City of London" | LAD14NM == "Barking and Dagenham" | ///
						LAD14NM == "Barnet" | LAD14NM == "Bexley" | ///
						LAD14NM == "Brent" | LAD14NM == "Bromley" | ///
						LAD14NM == "Camden" | LAD14NM == "Croydon" | ///
						LAD14NM == "Ealing" | LAD14NM == "Enfield" | ///
						LAD14NM == "Greenwich" | LAD14NM == "Hackney" | ///
						LAD14NM == "Hammersmith and Fulham" | LAD14NM == "Haringey" | ///
						LAD14NM == "Harrow" | LAD14NM == "Havering" | ///
						LAD14NM == "Hillingdon" | LAD14NM == "Hounslow" | ///
						LAD14NM == "Islington" | LAD14NM == "Kensington and Chelsea" | ///
						LAD14NM == "Kingston upon Thames" | LAD14NM == "Lambeth" | ///
						LAD14NM == "Lewisham" | LAD14NM == "Merton" | ///
						LAD14NM == "Newham" | LAD14NM == "Redbridge" | ///
						LAD14NM == "Richmond upon Thames" | LAD14NM == "Southwark" | ///
						LAD14NM == "Sutton" | LAD14NM == "Tower Hamlets" | ///
						LAD14NM == "Waltham Forest" | LAD14NM == "Wandsworth" | ///
						LAD14NM == "Westminster"				
						
gen treat = .						
replace treat = 0 if 	LAD14NM == "Epping Forest" 	| LAD14NM == "Broxbourne" | ///
						LAD14NM == "Welwyn Hatfield" 	| LAD14NM == "Hertsmere" | ///
						LAD14NM == "Three Rivers" 	| LAD14NM == "South Bucks" | ///
						LAD14NM == "Spelthorne" 	| LAD14NM == "Elmbridge" | ///
						LAD14NM == "Epsom and Ewell" 	| LAD14NM == "Reigate and Banstead" | ///
						LAD14NM == "Tandridge" 	| LAD14NM == "Sevenoaks" | ///
						LAD14NM == "Dartford" 	| LAD14NM == "Thurrock" | ///
						LAD14NM == "Brentwood"  | LAD14NM == "Slough" | ///
						LAD14NM == "Thurrock" 
replace treat = 1 if 	LAD14NM == "Barnet" | LAD14NM == "Bexley" | ///
						LAD14NM == "Bromley" | LAD14NM == "Croydon" | ///
						LAD14NM == "Enfield" | LAD14NM == "Harrow" | ///
						LAD14NM == "Havering" | LAD14NM == "Hillingdon" | ///
						LAD14NM == "Hounslow" | LAD14NM == "Kingston upon Thames" | ///
						LAD14NM == "Redbridge" | LAD14NM == "Richmond upon Thames" |  ///
						LAD14NM == "Sutton" 	| LAD14NM == "Waltham Forest"  


preserve
collapse (first) london treat , by(id_map)

label define london 0 "South-east" 1 "London", modify
label values london london

label define treat 0 "SE next to London" 1 "Outer London", modify
					 
label values treat treat

spmap london using "${datadir}\gbcoord", id(id_map) clmethod(unique) ndlabel("not included") legend(size(*1.5) position(9)) ///
		 graphregion(color(white)) name(london, replace) //

spmap treat using "${datadir}\gbcoord", id(id_map) clmethod(unique) ndlabel("not included") legend(size(*1.5) position(9)) ///
		graphregion(color(white)) name(treat, replace)  //

gr combine london treat, graphregion(color(white))

gr export "${figuredir}\map_border.png", as(png)  width(800) height(600) replace
restore	

	
}
*

*------------------------------------------------------------------------------*
* Regressions *
*------------------------------------------------------------------------------*

if ${reg} == 1 {


*--- Get regional house prices (as control)
drop index
merge m:1 regionname datem using ///
	"${datadir}\local_price_appreciation", ///
	keepusing(index) gen(merge_region) keep(1 3)

*--- Deflated house prices
gen rescale_index = .
forvalues i = 1/9 {
	sum index if datem == ym(2015, 8) & region == `i'
	replace rescale_index = index / r(mean) * 100 if region == `i'
}
gen defprice = purchaseprice / rescale_index * 100

*--- Square meters
drop if mi(psd_id)
merge 1:1 psd_id using "${datadir}\HTB_main_feb2016_sqft", nogen
ren total_floor_area total_floor_area1


*--- Run diff-in-diff with Treat-Control
eststo clear
foreach x in $myvarlist total_floor_area  { // hti
	eststo `x': reg `x' london##post rescale_index ${regvar} if treat!=., cluster(postcode3)
	estadd local borrower "Yes"
	estadd local hp "Yes"
}


// final version (no monthly payment, added square meters)
esttab ${myvarlist} total_floor_area ///
    using "${tabledir}\feb2016_did_SE_rescale_border.tex" ///
	, se b(2) ///
	keep(1.london 1.post 1.london#1.post) ///
	order(1.london#1.post 1.london 1.post) ///
	star(* 0.10 ** 0.05 *** 0.01) ///
	coeflabel(1.london#1.post		"\textsc{\textbf{London $\times$ Post Jan 2016}}" ///
			1.london				"\textsc{London}" ///
			1.post 					"\textsc{Post Jan 2016}") ///
	stats(borrower hp r2 N, fmt (0 0 2 %7.0fc) ///
	labels( `"\textsc{Borrower characteristics}"' `"\textsc{Regional house price index}"' ) ) ///
	mtitles("\textsc{\shortstack{Equity\\loan}}" "\textsc{\shortstack{Purchase\\price}}" ///
		"\textsc{\shortstack{Down\\payment}}" "\textsc{\shortstack{Mortgage\\amount}}" ///	
		"\textsc{\shortstack{Deflated\\purchase price}}" ///
		"\textsc{\shortstack{Square\\meters}}" ) replace ///
	nogap nonotes 


	
	

*--- Run diff-in-diff with south east only (placebo)
	
gen closetolondon = . 
replace closetolondon = 1 if treat ==0 & london==0					
replace closetolondon = 0 if treat ==. & london==0					

eststo clear
foreach x in $myvarlist total_floor_area {
	eststo `x': reg `x' closetolondon##post rescale_index ${regvar} if closetolondon!=., cluster(postcode3)
	estadd local borrower "Yes"
	estadd local hp "Yes"
}
esttab ${myvarlist} total_floor_area using "${tabledir}\feb2016_did_ctrl2_SE_rescale_border_placebo.tex", se b(2) ///
	keep(1.closetolondon 1.post 1.closetolondon#1.post) ///
	order(1.closetolondon#1.post 1.closetolondon 1.post) ///
	star(* 0.10 ** 0.05 *** 0.01) ///
	coeflabel(1.closetolondon#1.post	"\textsc{\textbf{Close to London $\times$ Post Jan 2016}}" ///
			1.closetolondon				"\textsc{Close to London}" ///
			1.post 						"\textsc{Post Jan 2016}") ///
	stats(borrower hp r2 N, fmt (0 0 2 %7.0fc) ///
	labels( `"\textsc{Borrower characteristics}"' `"\textsc{Regional house price index}"' ) ) ///
	mtitles("\textsc{\shortstack{Equity\\loan}}" "\textsc{\shortstack{Purchase\\price}}" ///
		"\textsc{\shortstack{Down\\payment}}" "\textsc{\shortstack{Mortgage\\amount}}" /// 
		"\textsc{\shortstack{Deflated\\purchase price}}" ///
		"\textsc{\shortstack{Square\\meters}}" ) replace ///
	mgroups("Values in \pounds1,000", pattern(1 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span ) /// 
	nogap nonotes 
		
}
*



*--- Parallel trend Charts
local vars $myvarlist total_floor_area1 
local ylabs `" "Equity loan" "Purchase price" "Down payment" "Mortgage amount" "Deflated purchase price" "Square meters" "'
local n: word count `vars'
forvalues i = 1/`n' {
	local var: word `i' of `vars'
	local ylab: word `i' of `ylabs'
	
	if "`var'" == "loan_final" {
		local ytick "ylab(200(25)300)"
	}
	else if "`var'" == "deposit_final" {
		local ytick "ylab(20(10)50)"
	}
	else {
		local ytick
	}
	
	preserve
	keep if treat != .
	collapse `var', by(datem london)

	*--- Chart
	format %tm datem
	line `var' datem if london == 0, lwidth(medthick) || ///
		line `var' datem if london == 1, lwidth(medthick) ///
		lpattern(dash) `ytick' ///
		${graphoption} xtitle("") xlabel(667(3)678)  ///
		ytitle("`ylab'") ///
		leg(order(1 "South-East" 2 "London")) ///
		xline(673, lpattern(dash) lcolor(black)) ///
		name(`var', replace)
	restore
}
gr combine $myvarlist total_floor_area1, r(2) scale(0.7) ///
	graphregion(color(white)) imargin(10 10 10 10) 
graph export "${projectdir}\Figure\South-East_London_border_ptrend.pdf", ///
		as(pdf) replace
